package com.xrit.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.constant.MessageConstant;
import com.xrit.exception.CustomException;
import com.xrit.utils.POIUtils;
import com.xrit.pojo.OrderSetting;
import com.xrit.result.Result;
import com.xrit.service.OrderSettingService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference  //远程调用
    private OrderSettingService orderSettingService;

    @RequestMapping("/setNumberByOrderDate")
    public Result setNumberByOrderDate(@RequestBody OrderSetting orderSetting) throws CustomException{
        orderSettingService.updateNumberByOrderDate(orderSetting);
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }

    /**
     * 根据年份和月份获取预约设置的数据
     * @param year
     * @param month
     * @return
     */
    @RequestMapping("/getOrderSettingByDate")
    public Result getOrderSettingByDate(Integer year,Integer month){
        try {
            List<Map> maps = orderSettingService.getOrderSettingByDate(year, month);
            if (maps!=null && maps.size()>0){
                return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,maps);
            }else{
                return new Result(true, MessageConstant.GET_ORDERSETTING_ISNULL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //文件解析失败
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 上传文件
     * @param excelFile
     * @return
     * @throws CustomException
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile) throws CustomException {
        //判断excelFile对象不为空并且携带数据
        if (excelFile!=null && !excelFile.isEmpty()){
            try {
                //1.使用poi工具类读取文件内容
                List<String[]> list = POIUtils.readExcel(excelFile);
                //2.将List<String[]>转成List<OrderSetting>
                List<OrderSetting> orderSettings=new ArrayList<>();
                for (String[] strings:list) {
                    OrderSetting os=new OrderSetting();
                    os.setOrderdate(new Date(strings[0]));
                    os.setNumber(Integer.parseInt(strings[1]));
                    orderSettings.add(os);
                }
                //3.添加到数据库中
                orderSettingService.add(orderSettings);
                return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_FAIL);
    }
}
